﻿using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using DimensionsHelper.Common.Contracts.Excel;

namespace DimensionsHelper.Common.Utils.Converters;

public class ExcelStyleColorJsonConverter : JsonConverter<ExcelStyleColor>
{
    public override ExcelStyleColor Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        string? text = reader.GetString();
        return !string.IsNullOrEmpty(text) && ExcelStyleColor.TryParse(text, out var color)
            ? color
            : ExcelStyleColor.Empty;
    }


    public override void Write(Utf8JsonWriter writer, ExcelStyleColor value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString());
    }
}